<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Role as RoleModel;
use sand\handle;
use app\admin\controller\Base;
use app\admin\controller\Admin;
//use think\Loader;
/**
* 
*/
class Role extends Base
{
    public function index(){
    	$role=RoleModel::paginate(10);
       // dump($users->render());exit;
    	$this->assign('role',$role);
        return $this->fetch();

    }
    public function add(){
    	return $this->fetch();
    }

    public function doadd(){
    	$user=[
    		'name'=>$_POST['name']
    	];
    	$validate=validate('Admin');
    	if(!$validate->scene('role')->check($user))
    		$this->error($validate->getError()); 
        if(!model('role')->save($user))
            $this->error("角色添加失败");
        else $this->success("角色添加成功");   
    }
    public function dodelete(){
        $id = input('param.id');
        $data = model('admin')->where("rid={$id}")->select();
        if(!$data){
            $rel = model('role')->where("id={$id}")->delete();
            if($rel)
                $this->success("角色删除成功");
            else $this->error("角色删除失败"); 
        }else{
            $this->error("无法删除该角色"); 
        }
    }
    public function doedit(){
        $id = input('post.id');
        $role = model('role')->get($id);
        $role->name = input('post.name');
        $rel = $role->save();
        if($rel)
            $this->success("角色修改成功");
        else
           $this->error("角色修改失败"); 

    }
    public function edit(){
        $id = input('post.id');
        $this->assign("id",$id);
        $data = model('role')->get($id);
        $this->assign("name",$data['name']);
        $rel = $this->fetch("edit");
        $json['status'] = 1;
        $json['html'] = $rel;
        echo json_encode($json);
        exit;
    }
}